package psn.zjk.exporter;

import psn.zjk.exporter.utils.Assert;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author sdyt_zhangjunkai
 * @version 1.0
 * @CLASS_NAME DynamicTableBuilder
 * @DESCRIPTION
 * @CREATE_TIME 2020/9/16 14:14
 */
public class DynamicTableBuilder extends ExcelDataBuilder {
    private HashSet<String> headers = new HashSet<>();
    private HashSet<String> rowHeaders = new HashSet<>();

    /**
     * @param rowHeaderField 列头字段
     * @param headerField    列字段
     * @param valueField     数据域字段
     * @METHOD_NAME: ExcelDataBuilder
     * @CREATE_TIME 2020/9/4 14:41
     * @RETURN
     * @AUTHOR sdyt_zhangjunkai
     */
    protected DynamicTableBuilder(String rowHeaderField, String headerField, String valueField) {
        super();
        Assert.hasLength(headerField, "headerField is empty!");
        Assert.hasLength(rowHeaderField, "rowHeaderField is empty!");
        Assert.hasLength(valueField, "valueField is empty!");
        //列头
        excelData.headerField = headerField;
        //行头
        excelData.rowHeaderField = rowHeaderField;
        //数据起始单元格列位置
        excelData.valueField = valueField;

    }


    @Override
    public ExcelDataBuilder setDatas(List<?> datas) {
        datas.forEach(o -> {
            String header = getPropertyWithConvertNull(o, excelData.headerField).toString();
            header = getMappedShownHeaderOfRow(header);
            headers.add(header);
            String rowheader = getPropertyWithConvertNull(o, excelData.rowHeaderField).toString();
            rowheader = getMappedShownHeaderOfRow(rowheader);
            rowHeaders.add(rowheader);
        });
        return super.setDatas(datas);
    }

    @Override
    protected void assembleTmpPojo(SheetRow row, Object pojo) {
        if (pojo != null) {
            String headerField = getPropertyWithConvertNull(pojo, excelData.headerField).toString();
            String value = getPropertyWithConvertNull(pojo, excelData.valueField).toString();
            SheetColumn sheetColumn = column(headerField);
            cell(row, sheetColumn, value);
        }
    }

    @Override
    protected void assembleTmpPojo(SheetColumn column, Object pojo) {
        String rowHeaderField = getPropertyWithConvertNull(pojo, excelData.rowHeaderField).toString();
        String value = getPropertyWithConvertNull(pojo, excelData.valueField).toString();
        SheetRow row = row(rowHeaderField);
        cell(row, column, value);
    }

    @Override
    protected List<String> getHeaders() {
        return new ArrayList<>(headers);
    }

    @Override
    protected List<String> getRowHeaders() {
        return new ArrayList<>(rowHeaders);
    }

    /**
     * @METHOD_NAME: assembleDatasWithDynamicHeaders
     * @DESCRIPTION: 动态列头的表数据组装，一个单元格对应一条数据
     * @CREATE_TIME 2020/9/2 15:27
     * @RETURN void
     * @AUTHOR sdyt_zhangjunkai
     */
    protected void assembleDatas() {
        //列头需要去重、有序
        SheetRow firstRow = row(excelData.firstRow);
        SheetColumn firstClolumn = column(excelData.firstColumn);
        SheetRow sheetRow = null;

        int col = excelData.firstDataColumnIndex;
        SheetColumn sheetColumn = null;
        for (Object pojo : excelData.datas) {
            // 行头取值
            Object rowValue = getPropertyWithConvertNull(pojo, excelData.rowHeaderField);
            // 列头取值
            Object colValue = getPropertyWithConvertNull(pojo, excelData.headerField);
            // 表数据
            Object value = getPropertyWithConvertNull(pojo, excelData.valueField);

            String colValueStr = colValue.toString();
            sheetColumn = uniqueHeaders.get(colValueStr);

            String rowValueStr = rowValue.toString().intern();
            sheetRow = uniqueRows.get(rowValueStr);
            cell(sheetRow, sheetColumn, value);
        }
    }

}
